//
//  VOCListViewController.m
//  iVoc
//
//  Created by Wallace Wu on 09-3-18.
//  Copyright 2009 Sonic Solutions. All rights reserved.
//

#import "VOCWordListCell.h"
#import "VOCSingleListViewController.h"
#import "VOCWordViewController.h"
#import "VocListController.h"
#import "iVocAppDelegate.h"
#import "UISearchBar+VOCExtentions.h"
#import "VOCMutableWordList.h"


@implementation VOCSingleListViewController

@synthesize searchResult;
@synthesize list;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
	self.navigationItem.title = list.name;
    [tableView reloadData];
}

-(void)viewWillDisappear:(BOOL)animated
{
    searchBar.text = @"";
    [searchBar resignFirstResponder];
	self.navigationItem.title = nil;
    [super viewWillDisappear:animated];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillApear:) name:UIKeyboardWillShowNotification object:self.view.window];
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addWords:)];
	[self.navigationItem.rightBarButtonItem release];
    
    if(self.list == [VOCMutableWordList favoriteList])
    {
        showFavoriteStars = NO;
    }
    else
    {
        showFavoriteStars = YES;
    }
}

-(void)addWords:(id)sender
{
	
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [searchBar release];
    [tableView release];
    [searchResult release];
    [super dealloc];
}

-(void)keyboardWillApear:(NSNotification*)notification
{
    [searchBar changeReturnKey];
    
    //only change it once
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(inSearch)
    {
        return [searchResult count];
    }
    else
    {
        return [self.list.data count];        
    }

}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    VOCWordListCell *cell = (VOCWordListCell*)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[VOCWordListCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    NSUInteger index = [indexPath indexAtPosition:1];
    VOCWordData *word;
    if(!inSearch)
    {
        word = [self.list.data objectAtIndex:index];        
    }
    else
    {
        word = [self.searchResult objectAtIndex:index];
    }
    
    if([[VOCMutableWordList favoriteList] containsWord:word] && showFavoriteStars)
    {
        cell.showStar = YES;
    }
    else
    {
        cell.showStar = NO;
    }

    
    // Set up the cell...
    [cell setText:word.word];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    VOCWordData *word;
    if(inSearch)
    {
        word = [self.searchResult objectAtIndex:[indexPath indexAtPosition:1]];
    }
    else
    {
        word = [self.list.data objectAtIndex:[indexPath indexAtPosition:1]];
    }
    
    VOCWordViewController *wordViewController = [[VOCWordViewController alloc] initWithNibName:@"VOCWordViewController" bundle:nil];
    wordViewController.singleWord = word;
	[self.navigationController pushViewController:wordViewController animated:YES];
	[wordViewController release];
}

#pragma mark Search Bar delegates

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    inSearch = YES;
    
    self.searchResult = [NSArray array];
    
    [tableView reloadData];
}

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
    self.searchResult = [list findWordsWithString:searchText caseSentitive:NO];
    [tableView reloadData];
    
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)theSearchBar
{
    inSearch = NO;
    
    [tableView reloadData];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
    // here just dismiss the keyboard because there is some little hack of UISearchBar
    [theSearchBar resignFirstResponder];
    theSearchBar.text = @"";
}



@end
